//题目链接：https://leetcode.cn/problems/linked-list-cycle-ii/
class Solution 
{
public:
    ListNode *detectCycle(ListNode *head) 
    {
        //快慢指针，有环必定相遇
        ListNode* slow=head,*fast=head;
        while(fast&&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)//二者相遇，有环
            {
                ListNode* ptr=head;
                while(ptr!=slow)//找到入环节点
                {
                    slow=slow->next;
                    ptr=ptr->next;
                }
                return ptr;
            }
        }
        return nullptr;
    }
};